import { _decorator, Component, instantiate, Node, Vec3 } from 'cc';
import { ResMgr } from '../Mgr/ResMgr';
import { AssetList } from '../Global';
import { SingleControlCube } from './SingleControlCube';
import { GAMEDATA } from './GAMEDATA';
import { UIMgr } from '../Mgr/UIMgr';
import { GameScene } from './GameScene';
import { ModelLoad } from './ModelLoad';
const { ccclass, property } = _decorator;

@ccclass('ControlCubeLoad')
export class ControlCubeLoad extends Component {

    //操控方块数量
    private ControlCubeNum: number = 3;

    //单个操作方块数组
    public SingleControlCube: Node[] = [];

    //背景层
    @property(Node)
    public CubeBgLoad: Node = null;

    protected start(): void {

        for (let i = 0; i < this.ControlCubeNum; i++) {
            let sc: Node = instantiate(ResMgr.Instance.Prefabs[AssetList.Prefabs.SingelControl]);
            this.node.addChild(sc);
            sc.angle = 45;
            sc.setPosition(new Vec3(-210 + i * 210));
            this.SingleControlCube.push(sc);
        }

        this.AllUpdate();

    }

    /**
     * 整体更新
     */
    public AllUpdate(_isall: boolean = false) {
        UIMgr.Instance.ShowTop(AssetList.Top.NoTouch);

        if (!_isall) {
            for (let sc of this.SingleControlCube) if (!sc.getComponent(SingleControlCube).IsPutSucess) {
                UIMgr.Instance.HideTop(AssetList.Top.NoTouch);
                return;
            }
        }

        this.SingleControlCube.forEach((item) => {
            let cube_num: number = GAMEDATA.ReturnCubeNum();
            item.getComponent(SingleControlCube).UpdateControlCube(cube_num);
        });
        this.scheduleOnce(() => {
            UIMgr.Instance.HideTop(AssetList.Top.NoTouch);
        }, 0.5);
    }

    /**
     * 检查未使用得能不能放的下
     */
    public CheckHavePutDown() {
        for (let sc of this.SingleControlCube) {
            if (sc.getComponent(SingleControlCube).CheckIamPutDown()) {
                console.warn("有可以放的下得游戏正常")

                if (GameScene.Instance.ModelLoad.getComponent(ModelLoad).StepNum == 0) {//没有步数也游戏结束
                    GameScene.Instance.GameOver();
                }

                return;
            }
        };

        GameScene.Instance.GameOver();

    }
}


